package com.guit.hrms.controller;

import com.guit.hrms.dto.academy.AddAcademyDTO;
import com.guit.hrms.po.academy.Academy;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.AcademyService;
import com.guit.hrms.vo.academy.AcademyVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/department/academy")
public class AcademyController {

    @Autowired
    AcademyService academyService;

    /**
     * 新增二级学院
     * @param addAcademyDTO
     * @return
     */
    @PostMapping
    public Result addAcademy(@Valid @RequestBody AddAcademyDTO addAcademyDTO){

        academyService.addAcademy(addAcademyDTO);
        return Result.success();
    }

    /**
     * 查询所有二级学院
     * @return
     */
    @GetMapping
    public Result queryAcademy(){
        List<AcademyVO> academyList = academyService.getAcademy();
        return Result.success(academyList);
    }


    /**
     * 更新二级学院
     * @param academy
     * @return
     */
    @PatchMapping
    public Result updateAcademy(@RequestBody Academy academy){
        log.info("Received academy update request: {}", academy);
        if (academy.getAcademyId() == null) {
            return Result.error("二级学院编号不能为空");
        }
        if (academy.getAcademyNumber() == null &&
                academy.getAcademyParentId() == null &&
                academy.getAcademyName() == null &&
                academy.getAcademyProfile() == null &&
                academy.getAcademyManagerId() == null &&
                academy.getAcademyLevel() == null) {
            return Result.error("请选择要更新的信息");
        }
        academyService.updateAcademy(academy);
        return Result.success();
    }


    /**
     * 批量删除二级学院
     * @param academyIds
     * @return
     */
    @DeleteMapping("/{academyIds}")
    public Result deleteAcademy(@PathVariable List<Integer> academyIds){
        if (academyIds == null || academyIds.isEmpty()) {
            return Result.error("请选择要删除的二级学院");
        }
        academyService.deleteAcademy(academyIds);
        return Result.success();
    }


}
